package com.java46.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.java46.entity.ServiceLarge;
import com.java46.entity.Vehicle;
import com.java46.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *  没有指定路径默认进入页面
 */
@Controller
public class IndexController {
    @Autowired
    private  IndexService indexService;

    /*
     * 默认进入index界面
     *
     * @author Tigger Wu
     * @date 2018/8/16 13:16
     * @param
     * @return
     */

    @RequestMapping("/")
    public String index(){
        return "indexs";
    }
    /*
     * 将预约界面所需要的服务类型信息封装为json格式发送到预约界面
     *
     * @author Tigger Wu
     * @date 2018/8/16 13:16
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/service",produces = "application/json;charset=utf-8")
    public  String contact(HttpServletRequest request, HttpServletResponse response, Model model) throws JsonProcessingException {
        response.setContentType("text/xml;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setCharacterEncoding("UTF-8");
        List<ServiceLarge>slist = indexService.queryService();
        // 2. 创建 ObjectMapper 对象
        ObjectMapper mapper= new ObjectMapper();
        //创建json字符串接收数据
        String jsonStr = mapper.writeValueAsString(slist);
        //返回json
        return jsonStr;
    }

    /*
     * 将预约界面所需要的车类型信息封装为json格式发送到预约界面
     *
     * @author Tigger Wu
     * @date 2018/8/16 13:30
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/service2",produces = "application/json;charset=utf-8")
    public  String contact2(HttpServletRequest request, HttpServletResponse response, Model model) throws JsonProcessingException {
        response.setContentType("text/xml;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setCharacterEncoding("UTF-8");
        List<Vehicle>vlist = indexService.queryVice();
        // 2. 创建 ObjectMapper 对象
        ObjectMapper mapper= new ObjectMapper();
        //创建json字符串接收数据
        String jsonStr =  mapper.writeValueAsString(vlist);
        //返回json
        return  jsonStr;
    }


}
